public class Solution213 {
    public int rob(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        if (nums.length == 1) {
            return nums[0];
        }
        int res;
        int pre = 0, cur = 0;
        for (int i = 0; i < nums.length - 1; i++) {
            int t = cur;
            cur = Math.max(pre + nums[i], cur);
            pre = t;
        }
        res = cur;
        pre = cur = 0;
        for (int i = 1; i < nums.length; i++) {
            int t = cur;
            cur = Math.max(pre + nums[i], cur);
            pre = t;
        }
        res = Math.max(res, cur);
        return res;
    }
}
